#ifndef JTHREADPOOL_P_H
#define JTHREADPOOL_P_H

#include "jthreadpool.h"
#include <QThreadPool>

// - class JThreadPoolPrivate -

class QThreadPoolPrivate;

class JThreadPoolEx : public QThreadPool
{
    Q_OBJECT
public:
    JThreadPoolEx(QObject *parent = 0);

private:
    Q_DISABLE_COPY(JThreadPoolEx)
    QThreadPoolPrivate *_d;
    friend class JThreadPoolPrivate;
};

// - class JThreadPoolPrivate -

class JTHREADPOOL_EXPORT JThreadPoolPrivate
{
    J_DECLARE_PUBLIC(JThreadPool)
public:
    JThreadPoolPrivate(JThreadPool *jthreadPool, QThreadPool *other = 0, QObject *parent = 0)
        : q_ptr(jthreadPool)
        , threadPool(other)
    {
        if (threadPool == 0) {
            threadPool = new JThreadPoolEx(parent);
        }

        if (!inited) {
            threadPool->setMaxThreadCount(QThread::idealThreadCount() * 2);
            inited = true;
        }
    }

    ~JThreadPoolPrivate()
    {
        if (threadPool->inherits("JThreadPoolEx")) {
            qobject_cast<JThreadPoolEx *>(threadPool)->deleteLater();
        }
    }

    void stealRunnable(JRunnable *runnable);

private:
    QThreadPool *threadPool;
    static bool inited;
};

#endif // JTHREADPOOL_P_H
